<?php
/*
 module:		发布管理
 create_time:	2022-03-09 16:30:59
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;


use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeBasicRequest;
use AlibabaCloud\SDK\Ocrapi\V20210707\Ocrapi;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeAllTextRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Ocr extends Common
{

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Ocrapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => "LTAI5t6x3PvvhSKXDVMsTQMp",
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => "Dn1oovqw5lbLu0DbwoY8ltIYFWqJwo"
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        $config->endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Ocrapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($url)
    {
        $client = self::createClient("LTAI5t6x3PvvhSKXDVMsTQMp","Dn1oovqw5lbLu0DbwoY8ltIYFWqJwo");
        $recognizeBasicRequest = new RecognizeBasicRequest([
            "url" => $url
        ]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $res=$client->recognizeBasicWithOptions($recognizeBasicRequest, new RuntimeOptions([]));
            $data=json_decode(get_object_vars(get_object_vars($res)["body"])["data"],true)["content"];
            print_r($data);exit();
        } catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }

    public function index(){
        $url       = $this->request->get('url');
        $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
        if (file_exists($path)) {
            require_once $path;
        }
       // $argv="https://xcx.xizihongniang.cn/static/img/jrsb.png";
        Ocr::main($url);
    }




}

